package com.msite
import com.msite.web.IndexController
import org.junit.Test
import org.springframework.http.MediaType
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.result.MockMvcResultHandlers
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
import org.springframework.test.web.servlet.setup.MockMvcBuilders
/**
 * Created by tpeng on 2015/11/25.
 */
//@RunWith(SpringJUnit4ClassRunner.class)
//@SpringApplicationConfiguration(classes = MockServletContext.class)
//@WebAppConfiguration
class IndexControllerTest {

    private MockMvc mvc;

    //@Before
    public void setUp() throws Exception {
        mvc = MockMvcBuilders.standaloneSetup(new IndexController()).build();
    }

    /*
    perform：执行一个RequestBuilder请求，会自动执行SpringMVC的流程并映射到相应的控制器执行处理；

    andExpect：添加ResultMatcher验证规则，验证控制器执行完成后结果是否正确；

    andDo：添加ResultHandler结果处理器，比如调试时打印结果到控制台；

    andReturn：最后返回相应的MvcResult；然后进行自定义验证/进行下一步的异步处理；
     */
    //@Test
    public void getHello() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/sa/index_")
                .accept(MediaType.TEXT_HTML))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print())
                //.andExpect(content().string(equalTo("Greetings from Spring Boot!")));
    }

    @Test
    void groovyTruth() {
        // Boolean expressions
        def a = true
        def b = true
        def c = false
        assert a
        assert a && b
        assert a || c
        assert !c

        // Collections
        def numbers = [1, 2, 3]
        assert numbers //true, as numbers in not empty
        numbers = []
        assert !numbers //true, as numbers is now an empty collection

        // Iterators and Enumerations
        assert ![].iterator() // false because the Iterator is empty
        assert [0].iterator() // true because the Iterator has a next element
        def v = new Vector()
        assert !v.elements()  // false because the Enumeration is empty
        v.add(new Object())
        assert v.elements()   // true because the Enumeration has more elements

        // Maps
        assert ['one': 1]
        assert ![:]

        // Matchers
        assert ('Hello World' =~ /World/) //true because matcher has at least one match

        // Strings
        assert 'This is true'
        assert !''
        //GStrings
        def s = ''
        assert !("$s")
        s = 'x'
        assert ("$s")

        // Numbers
        assert !0 //yeah, 0s are false, like in Perl
        assert 1  //this is also true for all other number types

        // Object references
        assert new Object()
        assert !null
    }

}
